package com.crt.nexus.oauth.service;

import com.crt.nexus.oauth.authorization.AuthenticationContext;
import com.crt.nexus.security.domain.bean.ClientBean;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RequiredArgsConstructor
public class UserAuthenticationManager implements AuthenticationManager {

    private final AuthenticationContext context;

    @Override
    public Authentication authenticate(Authentication authentication) {
        ClientBean client = (ClientBean) authentication.getDetails();
        return context.get(client.getGrantType()).authenticate(authentication);
    }

}
